layui.use(['form', 'table', 'layer', 'laydate'], function (form, table, layer, laydate) {

    form.render();

    //获取表单数据
    let searchCondition = "";

    function getSearchCodition() {
        return searchCondition;
    }

    const punchTable = table.render({
        elem: '#punchList',
        url: '../leaves/all',
        height: 460,
        cellMinWidth: 95,
        page: true,
        // limits: [10, 15, 20, 25],
        limit: 10,
        loading: true,
        contentType: "application/json;charset=utf-8",
        id: "punchListTable",
        method: 'post',
        where: getSearchCodition(),
        //res 即为原始返回的数据
        parseData: function (res) {
            console.log(res);
            return {
                "code": res.code,
                "count": res.count,
                "data": res.data
            };
        },
        done: function (res, curr, count) {
            $(".layui-laypage-limits").hide();
            if (curr > 1 && null == res.data) {
                curr = curr - 1;
                table.reload('paper-info', {
                    //设定异步数据接口的额外参数，任意设
                    where: {},
                    page: {
                        curr: curr
                    }
                });
            }
        },
        cols: [[
            {type: "radio", fixed: "left", width: 50},
            {field: 'id', title: 'id', minWidth: 50, align: "center", hide: true},
            {field: 'applyName', title: '申请人姓名', width: 100, minWidth: 100, align: "center"},
            {field: 'beginTime', title: '申请时间', minWidth: 149, align: "center"},
            {field: 'endTime', title: '假期结束时间', minWidth: 149, align: "center"},
            {field: 'reason', title: '请假理由', minWidth: 149, align: "center"},
            {field: 'days', title: '请假天数', minWidth: 149, align: "center"},
            {
                field: 'isAgreed', title: '状态', minWidth: 149, align: "center", templet: function (d) {
                    switch (d.isAgreed) {
                        case 0:
                            return '未审批';
                            break;
                        case 1:
                            return '<span style="background-color: #00FF00;opacity: 0.5;color: white">通过</span>';
                            break;
                        case 2:
                            return '<span style="background-color: #CF1900;opacity: 0.5;color: white">拒绝</span>';
                            break;
                    }
                }
            }
        ]]
    });

    //重新加载表格
    function reload() {
        //组装查询条件
        searchCondition = {
            searchCondition: {
                search: $("#search").val()
            }
        };
        //重新加载表格
        punchTable.reload({
            where: searchCondition,
            page: {
                curr: 1  //重新从第一页开始加载
            }
        });
    }

    //查询按钮监听
    form.on("submit(querySubmit)", function () {
        reload();
    });

    //同意按钮监听
    $(".agree").click(function () {
        //判断当前所选数据的数目
        var checkStatus = table.checkStatus('punchListTable');
        var length = checkStatus.data.length;
        if (length == 0) {
            layer.msg("请选择一条需要审批的请假信息", {time: 1000});
            return;
        }
        let data = checkStatus.data[0];
        updatePunch(data.id, 1);
    });

    //拒绝按钮监听
    $(".reject").click(function () {
        //判断当前所选数据的数目
        var checkStatus = table.checkStatus('punchListTable');
        var length = checkStatus.data.length;
        if (length == 0) {
            layer.msg("请选择一条需要审批的请假信息", {time: 1000});
            return;
        }
        let data = checkStatus.data[0];
        updatePunch(data.id, 2);
    });

    function updatePunch(leaveId, apply) {
        $.ajax({
            url: "../leaves/" + leaveId + "/" + apply,
            success: function (data) {
                if (data.obj) {
                    layer.msg(
                        '审批成功',
                        {
                            icon: 1,
                            time: 1000
                        },
                        function () {
                            window.location.reload();
                        });
                } else {
                    layer.msg('审批失败', {icon: 2});
                }
            }
        })
    }

});